package controller.worker;

import entity.User;
import util.JdbcDeal;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

public class Worker_add extends JDialog implements ActionListener {
    JPanel p1,p2;
    private JComboBox sex_Box; //”性别选择“

    //单行文本框组件
    private JTextField[] textFields;
    JButton btn_ok,btn_cancel;
    private JPasswordField password;	//密码文本域。

    public Worker_add() {
        this.setTitle("员工添加");
        this.setSize(400,350);
        this.setLocationRelativeTo(null);
        this.setLayout(new BorderLayout());
        this.setModal(true);    //模态窗口
        p1 = new JPanel();
        p1.setLayout(new FlowLayout());

        Box boxH; // 行式盒
        Box boxVOne, boxVTwo; // 列式盒

        boxH = Box.createHorizontalBox();   //水平
        boxVOne = Box.createVerticalBox();  //竖直
        boxVTwo = Box.createVerticalBox();
        boxVOne.add(Box.createVerticalStrut(50));
        boxVTwo.add(Box.createVerticalStrut(50));

        //JLabel标签组件
        JLabel[] labels = new JLabel[4];
        String[] name = {"工 号", "姓 名", "性 别", "密 码"};

        for (int i = 0; i < 4; i++) {
            labels[i] = new JLabel(name[i]);
            boxVOne.add(labels[i]);
            //固定高度的组件createVerticalStrut
            boxVOne.add(Box.createVerticalStrut(10));
        }

        textFields = new JTextField[2];
        for (int i = 0; i < 2; i++) {
            textFields[i] = new JTextField(15);
            boxVTwo.add(textFields[i]);
        }
        sex_Box = new JComboBox(new String[]{"", "男", "女"});
        boxVTwo.add(sex_Box);
        password = new JPasswordField(15);
        boxVTwo.add(password);


        boxH.add(boxVOne);
        //固定宽度的组件createHorizontalStrut
        boxH.add(Box.createHorizontalStrut(15));
        boxH.add(boxVTwo);
        p1.add(boxH);
        add(p1,BorderLayout.NORTH);

        boxH = Box.createHorizontalBox();
        boxVOne = Box.createVerticalBox();
        boxVTwo = Box.createVerticalBox();

        p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        btn_ok = new JButton("添加");
        btn_cancel = new JButton("取消");
        boxVOne.add(btn_ok);
        boxVOne.add(Box.createHorizontalStrut(25));
        boxVTwo.add(btn_cancel);
        boxVTwo.add(Box.createHorizontalStrut(25));
        boxH.add(boxVOne);
        boxH.add(boxVTwo);
        p2.add(boxH);
        add(p2,BorderLayout.CENTER);


        btn_ok.addActionListener(this);
        btn_cancel.addActionListener(this);
        this.setVisible(true);
        validate();


    }

    @Override
    public void actionPerformed(ActionEvent e) {
        String sex = sex_Box.getSelectedItem().toString();

        if(e.getSource() == btn_cancel){
            this.dispose();
            return;
        }

        //数据校验部分
        if(this.textFields[0].getText().equals("")){
            JOptionPane.showMessageDialog(this, "员工编号不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }
        if(this.textFields[1].getText().equals("")){
            JOptionPane.showMessageDialog(this, "姓名不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }
        if(new String(this.password.getPassword()).equals("")){
            JOptionPane.showMessageDialog(this, "密码不能为空！", "", JOptionPane.WARNING_MESSAGE);
            return ;
        }


        //业务处理
        if(e.getSource() == btn_ok){

            User add_user = new User();
            add_user.setWorkNo(this.textFields[0].getText());
            add_user.setName(this.textFields[1].getText());
            add_user.setPassword(new String(this.password.getPassword()));
            add_user.setSex(sex);
            JdbcDeal jdbcDeal = new JdbcDeal();
            try {
                if(jdbcDeal.Worker_add(add_user) == 0){
                    JOptionPane.showMessageDialog(this, "添加失败(可能已存在工号)", "", JOptionPane.WARNING_MESSAGE);
                    this.textFields[0].setText("");
                    this.textFields[1].setText("");
                    sex_Box.setSelectedIndex(0);
                    this.password.setText("");
                    jdbcDeal.close();
                    return ;
                }else{
                    JOptionPane.showMessageDialog(this,"添加成功","",JOptionPane.PLAIN_MESSAGE);
                    this.dispose();
                    jdbcDeal.close();
                    return;
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
    }

}
